<?php
/*
 * Copyright (c) 2021.  Liuhongbin All rights reserved.
 * @domain http://dkyx.cc
 * @author liuhongbin <514660094@qq.com>
 */

namespace app\controller\adminapi\platform\douyin;


use app\controller\adminapi\platform\PlatformController;
use app\common\controller\AdminController;
use app\common\model\platform\PlatformAddon;
use hiduoke\services\FormBuilder as Form;
use think\App;

// 添加应用 - 选择应用

// 应用列表


class DouyinAddon extends PlatformController
{
    /**
     * @var PlatformAddon
     */
    private PlatformAddon $model;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new PlatformAddon();
    }

    /**
     * 应用列表
     */
    public function index()
    {
        $res = $this->model->getPageList();
        return $this->success($res['count'], $res['data']->append());
    }

    /**
     * 添加平台应用
     * @param $aid
     * @param $plaid
     */
    public function addPlatformAddon($aid, $addon_name, $plaid)
    {
        $platform_addon = $this->model->where(['addon_name' => $addon_name, 'plaid' => $plaid])->findOrEmpty();
        if (!$platform_addon->isEmpty()) {
            return $this->error('当前应用已添加');
        } else {
            $platform_addon->aid = $aid;
            $platform_addon->plaid = $plaid;
            $platform_addon->addon_name = $addon_name;
            $platform_addon->save();
            return $this->success('添加成功');
        }
    }

    public function getAddonForm()
    {
        $id = input("id", 0);
        $rule = array();
        $form = Form::createForm("/adminapi/platform/wechat/addon/save")->setMethod("POST");
        $row = $this->model->where(['id' => $id])->findOrEmpty();
        $rule[] = Form::input('id', 'ID', $row->id)->disabled(true);
        $rule[] = Form::input('title', '应用名称', $row->title);
        $rule[] = Form::input('icon', '图标', $row->icon);
        $rule[] = Form::textarea('description', '描述', $row->description)->rows('5');
        $form->setTitle("编辑")->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    public function save()
    {

    }

    public function del()
    {

    }

}
